(function() {
  
  function getScript(src) {
    document.write('<' + 'script src="' + src + '"' +
                   ' type="text/javascript"><' + '/script>');
  }
  getScript("http://api-maps.yandex.ru/1.1/index.xml?key=AJy6-U0BAAAAJr_5MgIAbUmUfURJX0QfQef4pLZO5vNlHjsAAAAAAAAAAACsZenVuLEihxbcpybjyqyczHDMyA==");
})();

function isMarkerVisible() {
   return (marker != null);
}

function fillUrls() {
   var markerlat = null;
   var markerlng = null;
   if (isMarkerVisible()) {
      markerlat = marker.getCoordPoint().getY();
      markerlng = marker.getCoordPoint().getX();
   }
   var maptype = (map.getType().getName() == 'Схема' ? 'map' : 'sat');
   fillUrlsCommon(map.getCenter().getY(), map.getCenter().getX(), map.getZoom(), maptype, markerlat, markerlng);
}

function processMarker(isGPS) {
   if (isGPS) {
      if (isMarkerVisible()) {
         map.removeOverlay(marker);
         marker = null;
      }
      gps_coords = new YMaps.GeoPoint(gps_lng, gps_lat);
      addMarker(gps_coords);
      map.setCenter(gps_coords, map.getZoom(), map.getType());
   } else {
      if (isMarkerVisible()) {
         map.removeOverlay(marker);
         marker = null;
      } else {
         var markerCoordinates = new YMaps.GeoPoint(map.getCenter().getX(), map.getCenter().getY());
         addMarker(markerCoordinates);
      }
   }
   fillUrls();
   markerButtons(isGPS);
}

function addMarker(markerCoordinates) {
   marker = new YMaps.Placemark(markerCoordinates, {draggable: true});
   map.addOverlay(marker);
   YMaps.Events.observe(marker, marker.Events.DragEnd, fillUrls);
   YMaps.Events.observe(marker, marker.Events.BalloonOpen, function () {
      marker.closeBalloon();
   });
}


function initialize() {

   initMapCanvasSize();

   var lat = getParameter('lat');
   if (lat == null || isNaN(lat))
      lat = def_lat;
   var lng = getParameter('lng');
   if (lng == null || isNaN(lat))
      lng = def_lng;
   var z = parseInt(getParameter('z'));
   if (z == null || isNaN(z))
      z = def_zoom;

   var mapType = getParameter('t');
   if (mapType == null)
      mapType = YMaps.MapType.MAP;
   else 
      if (mapType == 'sat')
        mapType = YMaps.MapType.HYBRID;
      else 
        mapType = YMaps.MapType.MAP;

   var mlat = getParameter('mlat');
   var mlng = getParameter('mlng');
   var markerCoordinates = null;
   if (mlat != null && !isNaN(mlat) && mlng != null && !isNaN(mlng)) {
      markerCoordinates = new YMaps.GeoPoint(mlng, mlat);
   }
      

   var mapCenter = new YMaps.GeoPoint(lng,lat);
   
   map = new YMaps.Map(document.getElementById("map_canvas"));
   map.setCenter(mapCenter, z, mapType);

   
   map.addControl(new YMaps.TypeControl());
   map.addControl(new YMaps.Zoom());
   map.addControl(new YMaps.ScaleLine());
   map.enableScrollZoom();

   YMaps.Events.observe(map, map.Events.Update, fillUrls);
   YMaps.Events.observe(map, map.Events.MoveEnd, fillUrls);
   YMaps.Events.observe(map, map.Events.DragEnd, fillUrls);
   YMaps.Events.observe(map, map.Events.MultiTouchEnd, fillUrls);
   YMaps.Events.observe(map, map.Events.SmoothZoomEnd, fillUrls);

   YMaps.Events.observe(map, map.Events.TypeChange, fillUrls);


   // MARKER
   if (markerCoordinates != null) {
      addMarker(markerCoordinates);
   }


   // init methods
   fillUrls();
   initMarkerButtons(isMarkerVisible());
   pageOnLoad();
}

function resizeMap() {
   map.redraw(true, null);
   fillUrls();
}